package seven.test_7_14;

import java.util.List;
import java.util.Stack;

public class Solution {
    public String solve (String s, String t) {
        // write code here
        int sl = s.length();
        int tl = t.length();
        int a = 0;
        if(sl <= tl) {
            for (int i = 0; i < sl; i++) {
                int i1 = s.charAt(i) - '0';
                int i2 = t.charAt(i) - '0';
                a += (i2 * (int)Math.pow(10,tl - 1 - i));
                a += (i1 * (int)Math.pow(10,sl - 1 - i));
            }
            for (int i = sl; i < tl; i++) {
                int i2 = t.charAt(i) - '0';
                a += (i2 * (int)Math.pow(10,tl - 1 - i));
            }
        } else {
            for (int i = 0; i < tl; i++) {
                int i1 = s.charAt(i) - '0';
                int i2 = t.charAt(i) - '0';
                a += (i2 * (int)Math.pow(10,tl - 1 - i));
                a += (i1 * (int)Math.pow(10,sl - 1 - i));
            }
            for (int i = tl; i < sl; i++) {
                int i2 = s.charAt(i) - '0';
                a += (i2 * (int)Math.pow(10,sl - 1 - i));
            }
        }


        String str = String.valueOf(a );
        return str;
    }
    public ListNode addInList (ListNode head1, ListNode head2) {
        // write code here
        Stack<Integer> stack1 = new Stack<>();
        Stack<Integer> stack2 = new Stack<>();
        while (head1 != null) {
            stack1.push(head1.val);
            head1 = head1.next;
        }
        while (head2 != null) {
            stack2.push(head2.val);
            head2 = head2.next;
        }
        int count = 0;
        int tmp = 0;
        int s1 = stack1.size();
        int s2 = stack2.size();
        if(s1 <= s2) {
            for (int i = 0; i < s1; i++) {
                int t1 = stack1.peek();
                int t2 = stack2.peek();
                count = count + Math.abs(t1 + t2 + tmp - 10) * (int)Math.pow(10,i);
                if(t1 + t2 + tmp >= 10) {
                    tmp = 1;
                } else {
                    tmp = 0;
                }
            }
            for (int i = s1; i < s2; i++) {
                int t2 = stack2.peek();
                count = count + Math.abs(t2 + tmp - 10) * (int)Math.pow(10,i);
                if(t2 + tmp >= 10) {
                    tmp = 1;
                } else {
                    tmp = 0;
                }
            }
            count = count + tmp + (int)Math.pow(10,s2);
        } else {
            for (int i = 0; i < s2; i++) {
                int t1 = stack1.peek();
                int t2 = stack2.peek();
                count = count + Math.abs(t1 + t2 + tmp - 10) * (int)Math.pow(10,i);
                if(t1 + t2 + tmp >= 10) {
                    tmp = 1;
                } else {
                    tmp = 0;
                }
            }
            for (int i = s2; i < s1; i++) {
                int t1 = stack1.peek();
                count = count + Math.abs(t1 + tmp - 10) * (int)Math.pow(10,i);
                if(t1 + tmp >= 10) {
                    tmp = 1;
                } else {
                    tmp = 0;
                }
            }
            count = count + tmp + (int)Math.pow(10,s2);
        }
        return getNode(count);
    }
    private ListNode getNode(int count) {
        ListNode head = new ListNode(0);
        ListNode cur = head;
        String str = String.valueOf(count);
        for (int i = str.length() - 1; i >= 0; i--) {
            int a = str.charAt(i) - '0';
            ListNode tmp = new ListNode(a);
            cur.next = tmp;
            cur = tmp;
        }
        return head.next;
     }
}
